/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.core.view.system;

import com.intelligent.ispc.client.core.controller.SystemConfigController;
import com.intelligent.ispc.client.core.modal.system.SettingsModal;
import com.intelligent.ispc.client.core.view.AbstractPanel;
import com.intelligent.ispc.client.ex.JTextFieldEx;
import com.intelligent.ispc.client.utils.FileUtils;
import com.intelligent.ispc.client.utils.GBC;
import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.common.utils.StringUtils;
import com.intelligent.ispc.constant.AppConstant;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by hao on 2016/9/13.
 */
@org.springframework.stereotype.Component
public class SystemConfigPanel extends AbstractPanel {

    private SettingsModal settingsModal;

    @Autowired
    private SystemConfigController systemConfigController;

    public SystemConfigPanel() {
    }

    @Override
    public void initView() {
        filePathTf.setText(settingsModal.getScriptPath());
        filePathOutTf.setText(settingsModal.getExportPath());
    }

    @Override
    public void initDataSource() {
        settingsModal = new SettingsModal();
        settingsModal.setExportPath(systemConfigController.getPath(AppConstant.SYSTEM_CONFIG_FIELD_PATH));
        settingsModal.setScriptPath(systemConfigController.getPath(AppConstant.SYSTEM_CONFIG_SCRIPT_PATH));
    }

    @Override
    @PostConstruct
    public void initComponents() {
        inputPanel = new JPanel();
        filePathLbl = new JLabel("Python Script");
        filePathTf = new JTextFieldEx();
        filePathOut = new JLabel("Export Path");
        filePathOutTf = new JTextFieldEx();
        inputPanel.setLayout(new GridBagLayout());

        JButton saveButton = new JButton();
        saveButton.setText("Save");
        JButton sysImportButton = new JButton();
        sysImportButton.setText("Browse");
        JButton exportImportButton = new JButton();
        exportImportButton.setText("Browse");

        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                saveConfig(evt);
            }
        });

        sysImportButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                sysImportButtonConfig(evt);
            }
        });

        exportImportButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                exportImportButtonConfig(evt);
            }
        });

        inputPanel.setLayout(new GridBagLayout());
        inputPanel.setBorder(BorderFactory.createTitledBorder("System Settings"));
        inputPanel.add(filePathLbl, new GBC(0, 0).setFill(GBC.HORIZONTAL).setWeight(5, 0).setInsets(5, 5, 0, 0));
        inputPanel.add(filePathTf, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0).setInsets(5, 5, 0, 0));
        inputPanel.add(sysImportButton, new GBC(2, 0).setFill(GBC.HORIZONTAL).setWeight(5, 0).setInsets(5, 5, 0, 0));

        inputPanel.add(filePathOut, new GBC(0, 1).setFill(GBC.HORIZONTAL).setWeight(5, 0).setInsets(5, 5, 0, 0));
        inputPanel.add(filePathOutTf, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(100, 0).setInsets(5, 5, 0, 0));
        inputPanel.add(exportImportButton, new GBC(2, 1).setFill(GBC.HORIZONTAL).setWeight(5, 0).setInsets(5, 5, 0, 0));


        inputPanel.add(saveButton, new GBC(1, 2).setAnchor(GBC.WEST).setInsets(5, 5, 5, 5));
        inputPanel.add(new JPanel(), new GBC(0, 3).setWeight(0, 100));

        this.setLayout(new GridBagLayout());
        this.add(inputPanel, new GBC(0, 0).setFill(GBC.HORIZONTAL).setAnchor(GBC.NORTH).setWeight(100, 100));
        this.setVisible(true);
    }

    private void saveConfig(ActionEvent evt) {
        try {
            if (!filePathTf.getText().isEmpty() && !filePathOutTf.getText().isEmpty()) {
                String scriptPath = filePathTf.getText();
                String exportPath = filePathOutTf.getText();
                if (systemConfigController.validatePath(AppConstant.SYSTEM_CONFIG_SCRIPT_PATH) || systemConfigController.validatePath(AppConstant.SYSTEM_CONFIG_FIELD_PATH)) {
                    systemConfigController.updatePath(AppConstant.SYSTEM_CONFIG_SCRIPT_PATH, scriptPath);
                    systemConfigController.updatePath(AppConstant.SYSTEM_CONFIG_FIELD_PATH, exportPath);
                } else {
                    systemConfigController.savePath(scriptPath, exportPath);
                }
                JOptionPane.showMessageDialog(null, "       Save system config succeed !", "Message", JOptionPane.PLAIN_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "       Please choose system config path !", "Error", JOptionPane.PLAIN_MESSAGE);
            }
        } catch (ApplicationException ex) {
        }
    }

    private void sysImportButtonConfig(ActionEvent evt) {
        String selectedFiles = FileUtils.filePathSelect(SystemConfigPanel.this, FileUtils.FILES_ONLY);
        selectedFiles = selectedFiles.replaceAll("\\\\", "/");
        if (!StringUtils.isBlank(selectedFiles)) {
            filePathTf.setText(selectedFiles);
        }
    }

    private void exportImportButtonConfig(ActionEvent evt) {
        String selectedFiles = FileUtils.filePathSelect(SystemConfigPanel.this, FileUtils.DIRECTORIES_ONLY);
        selectedFiles = selectedFiles.replaceAll("\\\\", "/");
        if (!StringUtils.isBlank(selectedFiles)) {
            filePathOutTf.setText(selectedFiles);
        }
    }

    private JPanel inputPanel;
    private JLabel filePathLbl;
    private JLabel filePathOut;
    private JTextField filePathTf;
    private JTextField filePathOutTf;
    private JPanel errorPanel;
}
